Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

191
Visualizações
I am storing strings in a 2D array. However when I print the result the [0][0] space is blank. Can someone tell me how to occupy that space?

How can I occupy the [0][0] space with information from the user. When it prints, it begins at [0][1] instead of [0][0]

Scanner scan = new Scanner(System.in);
int sizeArray;

//Prompting user for number of users to be added
System.out.println("How many users are you going to add?");
sizeArray = scan.nextInt(); //Reading users to add

String user [][] = new String[sizeArray][2];


System.out.println("Enter name followed by unique password: ");

for(int i = 0; i < sizeArray; i++) {
    for (int j = 0; j< 2; j++) {
        user[i][j] =scan.nextLine();
    }
}
System.out.println("You entered: ");

for(int i = 0; i < sizeArray; i++) {
    for(int j = 0; j < 2; j++) {
        System.out.println(" Name["+i+"]["+j+"] = "+user[i][j]);
    }
    System.out.print("");
}
over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

When it prints, it begins at [0][1] instead of [0][0]

you're invoking the scan.nextInt() method before scan.nextLine() which does not consume the last newline character of the user input hence the first scan.nextLine() within the loop consumes that newline character and when you attempt to retrieve the element at [0][0] within the user two-dimensional array, it will print an empty string.

a quick solution that will enable you to print the expect values starting at [0][0] is right after this:

System.out.println("Enter name followed by unique password: ");

insert this:

scan.nextLine();

now it becomes:

System.out.println("Enter name followed by unique password: ");
scan.nextLine(); // let this consume the newline character
over 4 years ago · Santiago Trujillo Relatório

0

 public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int sizeArray;

        System.out.println("How many users are you going to add?");
        sizeArray = Integer.parseInt(scan.nextLine());

        String user[][] = new String[sizeArray][2];


        System.out.println("Enter name followed by unique password: ");

        for (int i = 0; i < sizeArray; i++) {
            String[] input = scan.nextLine().split(" ");
            for (int j = 0; j < 2; j++) {
                user[i][j] = input[j];
            }
        }
        System.out.println("You entered: ");
        for (int i = 0; i < sizeArray; i++) {
            for (int j = 0; j < 2; j++) {
                System.out.println("Name[" + i + "][" + j + "] = " + user[i][j]);
            }
            System.out.print("");
        }
    }

I suppose this is the answer for your problem? :)

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda